
from whiff.middleware import misc
from whiff import gateway

class wedges(misc.utility):
    def __init__(self, path, url):
        self.path = path
        self.url = url
    def __call__(self, env, start_response):
        path = self.param_value(self.path, env).strip()
        url = self.param_value(self.url, env).strip()
        directoriesInfo = gateway.getResource(env, ["du", "dirs", path])
        fileInfo = gateway.getResource(env, ["du", "files", path])
        L = []
        # add the file info if non-zero
        if int(fileInfo)>0:
            fileJson = '{"value": %s, "title":"[files]", "pull_out":"true"}' % fileInfo
            L.append(fileJson)
        for dirInfo in directoriesInfo:
            [dirpath, bytes, filename] = dirInfo[:3]
            if int(bytes)>0:
                dirUrl = url + "?path=" + dirpath
                dirJson = '{"value": %s, "title":"%s", "url":"%s"}' % (
                    bytes, filename, dirUrl)
                L.append(dirJson)
        page = ",\n".join(L)
        page = "[\n%s\n]" % page
        #pr "wedges are"
        #pr page
        return self.deliver_page(page, env, start_response)

__middleware__ = wedges
